package test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.TreeMap;

import org.apache.http.client.ClientProtocolException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import com.wx.database.DBUtil;
import com.wx.entity.WechatTemplateMsg;
import com.wx.util.AccessToken;
import com.wx.util.WeiXinUtil;

import net.sf.json.JSONObject;

public class test {

	public static void main(String[] args) throws SQLException, UnsupportedEncodingException {
		// TODO Auto-generated method stub
		// 连接测试
		// Connection conn = DBUtil.getConnection();
		// Statement stmt = (Statement) conn.createStatement();
		// String sql = ""
		// + " SELECT tnum"
		// + " FROM temp;";
		// ResultSet rs = stmt.executeQuery(sql);
		// while (rs.next()) {
		// String str = rs.getString(1);
		//// String str = new String(rs.getString(1).getBytes("GBK"), "utf-8");
		// System.out.println(str);
		// }

		// AccessToken token;
		// try {
		// token = WeiXinUtil.getAccessToken();
		// System.out.println("Access_Token:" + token.getToken());
		// System.out.println("有效时间：" + token.getExpiresIn());
		//
		// String menu = JSONObject.fromObject(WeiXinUtil.initMenu()).toString();
		// System.out.println(menu.toString());
		// int result = WeiXinUtil.createMenu(token.getToken(), menu);
		// if (result == 0) {
		// System.out.println("菜单创建成功！");
		// } else {
		// System.out.println("菜单创建失败！");
		// }
		// } catch (ClientProtocolException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		//
		// try {
		// WeiXinUtil.getAccessToken2();
		// } catch (Exception e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
//		Connection conn = DBUtil.getConnection();
//		Statement stmt = (Statement) conn.createStatement();
//		String sql = "" + " INSERT INTO temp(time)" + " VALUES(?);";
//		PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql);
//
//		ptmt.setTimestamp(1, new Timestamp(new Date().getTime()));
//		ptmt.execute();
//		System.out.println(new Timestamp(new Date().getTime()));

		WechatTemplateMsg wts = new WechatTemplateMsg();
		TreeMap<String, TreeMap<String, String>> tm = new TreeMap<>();
		tm.put("first", WechatTemplateMsg.item("车牌记录通知", "#173177"));
		tm.put("keyword1", WechatTemplateMsg.item("小苑", "#173177"));
		tm.put("keyword2", WechatTemplateMsg.item("粤A2396A", "#173177"));
		tm.put("keyword3", WechatTemplateMsg.item(new Timestamp(new Date().getTime()).toString(), "#173177"));
		tm.put("remark", WechatTemplateMsg.item("谢谢使用本系统，祝你一路平安！", "#173177"));
		wts.setData(tm);
		wts.setTouser("omctT5_9Vs80Jk4n_Ua1nKRSh9e0");
		wts.setTemplate_id("JJiECzJdHviKcmbpHEsq590g0kymyLYhRKZEpVp5O6g");
		wts.setUrl("");
		try {
			WeiXinUtil.sendTemplateMsg(wts);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
